#include "trap.h"
#include "instr_def.h"
#include "instr_test.h"
    .set noat
    .globl main
    .text

main:
    li $k1, 0x0

    # BEQ 1
    li $k0, 0x0
    nop
    nop
    BEQ(0x11111111, 0x11111111, beq_point2)
    li $k0, 0x11111111
    nop
beq_point1:
    nop
    bne $k0, $zero, res_error
    nop
    nop
    BEQ(0xffffffff, 0x80000000, beq_end)
    li $k0, 0x00000001
    nop
    nop
    bne $k0, $zero, beq_end
    nop
beq_point2:
    nop
    bne $k0, $zero, res_error
    nop
    BEQ(0x80045aac, 0x80045aac, beq_point1)
    li $k0, 0x11111111
    nop
beq_end:
    nop
    li $k0, 0x00000001

    # BNE 2
    li $k0, 0x0
    nop
    nop
    BNE(0x11111111, 0xffffffff, bne_point2)
    li $k0, 0x22222222
    nop
bne_point1:
    nop
    bne $k0, $zero, res_error
    nop
    BNE(0x80000000, 0x80000000, bne_point2)
    li $k0, 0x00000002
    nop
    bne $k0, $zero, bne_end
    nop
    nop
bne_point2:
    nop
    bne $k0, $zero, res_error
    nop
    BNE(0x7fffffff, 0x00000000, bne_point1)
    li $k0, 0x22222222
    nop
bne_end:
    nop
    nop

    # BGEZ 3
    li $k0, 0
    BGEZ(0x00000000, bgez_point2)  # TEST ERROR
    li $k0, 0x33333333
bgez_point1:
    nop
    bne $k0, $zero, res_error
    nop
    nop
    BGEZ(0x80000000, bgez_point1)
    li $k0, 0x000000003
    nop
    nop
    j bgez_point3
bgez_point2:
    nop
    bne $k0, $zero, res_error
    nop
    nop
    BGEZ(0x7fffffff, bgez_point1)
    li $k0, 0x33333333
    nop
bgez_point3:
    nop
    beq $k0, $zero, res_error
    nop
    nop

    # BGTZ 4
    li $k0, 0
    BGTZ(0x7fffffff, bgtz_point1)  # TEST ERROR
    li $k0, 0x44444444
bgtz_point1:
    bne $k0, $zero, res_error
    nop
    nop
    BGTZ(0x00000000, bgtz_point2)
    li $k0, 0x00000004
bgtz_point2:
    beq $k0, $zero, res_error
    nop
    nop
    li $k0, 0
    nop
    nop
    BGTZ(0x80000000, bgtz_point3)
    li $k0, 0x00000004
bgtz_point3:
    beq $k0, $zero, res_error

    # BLEZ 5
    li $k0, 0
    nop
    nop
    BLEZ(0x00000000, blez_point2)
    li $k0, 0x55555555
    nop
blez_point1:
    nop
    bne $k0, $zero, res_error
    nop
    nop
    BLEZ(0x7fffffff, blez_point2)
    li $k0, 0x00000005
    nop
    j blez_point3
    nop
blez_point2:
    nop
    bne $k0, $zero, res_error
    nop
    nop
    BLEZ(0x80000000, blez_point1)
    li $k0, 0x55555555
    nop
blez_point3:
    nop
    beq $k0, $zero, res_error
    nop
    nop

    # BLTZ 6
    li $k0, 0
    nop
    nop
    BLTZ(0x80000000, bltz_point1)
    li $k0, 0x66666666
bltz_point1:
    bne $k0, $zero, res_error
    nop
    nop
    BLTZ(0x7fffffff, bltz_point2)
    li $k0, 0x00000006
bltz_point2:
    beq $k0, $zero, res_error
    nop
    nop
    li $k0, 0x0 
    nop
    nop
    BLTZ(0x00000000, bltz_point3)
    li $k0, 0x00000006
bltz_point3:
    beq $k0, $zero, res_error
    nop
    nop

    # BGEZAL 7
    li $k0, 0
    BGEZAL(0x00000000, bgezal_point2)
    li $k0, 0x77777777
bgezal_point1:
    bne $k0, $zero, res_error
    nop
    nop
    BGEZAL(0x80000000, bgezal_point3)
    li $k0, 0x00000007
    nop
    nop
    j bgezal_point3
bgezal_point2:
    bne $k0, $zero, res_error
    nop
    nop
    BGEZAL(0x7fffffff, bgezal_point1)
    li $k0, 0x77777777
    nop
    nop
bgezal_point3:
    beq $k0, $zero, res_error
    nop
    nop

    # BLTZAL 8
    li $k0, 0
    BLTZAL(0x80000000, bltzal_point2)
    li $k0, 0x88888888
bltzal_point1:
    bne $k0, $zero, res_error
    nop
    nop
    BLTZAL(0x7fffffff, bltzal_point1)
    li $k0, 0x00000008
    nop
    nop
    j bltzal_point3
bltzal_point2:
    bne $k0, $zero, res_error
    nop
    nop
    BLTZAL(0xffffffff, bltzal_point1)
    li $k0, 0x88888888
    nop
    nop
bltzal_point3:
    beq $k0, $zero, res_error
    nop
    nop

    # J 9
    li $k0, 0x00000009
    nop
    nop
j_point2:
    beq $k0, $zero, j_end
    nop
    nop
    J(j_point1)
    nop
    nop
    li $k0, 0x99999999
    nop
    nop
j_point1:
    li $k0, 0x0
    nop
    nop
    J(j_point2)
    nop
    nop
    li $k0, 0x99999999
    nop
    nop
j_end:
    li $k0, 0x00000009
    nop
    nop

    # JAL 10
    li $k0, 0x0000000a
    nop
    nop
jal_point2:
    beq $k0, $zero, jal_end
    nop
    nop
    JAL(jal_point1)
    nop
    nop
    li $k0, 0xaaaaaaaa
jal_point1:
    li $k0, 0x0
    nop
    nop
    JAL(jal_point2)
    nop
    nop
    li $k0, 0xaaaaaaaa
    nop
    nop
jal_end:
    li $k0, 0x0000000a
    nop
    nop

    # JR 11
    li $k0, 0x0000000b
    nop
    nop
jr_point2:
    nop
    beq $k0, $zero, jr_end
    nop
    nop
    JR(jr_point1)
    nop
    li $k0, 0xbbbbbbbb
    nop
    nop
jr_point1:
    nop
    and $k0, $zero, $zero
    nop
    JR(jr_point2)
    nop
    li $k0, 0xbbbbbbbb
    nop
    nop
jr_end:
    nop
    li $k0, 0x0000000b
    nop
    nop

    # JALR 12
    li $k0, 0x0000000c
    nop
    nop
jalr_point2:
    nop
    beq $k0, $zero, jalr_end
    nop
    nop
    JALR(jalr_point1)
    nop
    li $k0, 0xcccccccc
    nop
    nop
jalr_point1:
    nop
    and $k0, $zero, $zero
    nop
    JALR(jalr_point2)
    nop
    li $k0, 0xcccccccc
    nop
    nop
jalr_end:
    nop
    li $k0, 0x0000000b
    nop
    nop

    li $k1, 0x12345678
    HIT_GOOD_TRAP		# stop temu

res_error: 
    li $s1, 0x11111111
    HIT_BAD_TRAP		# stop temu
